<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2018/12/6
 * Time: 10:38
 */

namespace app\upload\controller;
use app\common\controller\Base;

/**
 * @title 上传（模块10-01）
 * @description 帖子说明
 * @group 上传（10）
 * @header name:model_id require:0 default:10-01 desc:模块
 */
class Upload extends Base
{
    /**
     * @title 前台上传图片到本地
     * @description 前台上传图片到本地(会返回图片保存的地址,将返回的地址传入其他接口即可)
     * @author 李锐志
     * @url /upload/upload/upload
     * @method *
     */
    public function upload()
    {
        // 获取表单上传文件
        $files = request()->file();

        if (!$files) {
            // 上传失败获取错误信息
            $msg['msg'] = '上传为空';
            return $this->errorJson($msg);
        }

        foreach ($files as $file) {
            // 移动到框架应用根目录/public/upload/ 目录下
            $dir = '/upload/';
            $extension = strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION));
            if ($extension) {
                $saveName = md5(microtime(true)) . "." . $extension;
            } else {
                $saveName = md5(microtime(true));
            }

            $data['oname'] = $file->getInfo('name');
            $data['isimg'] = $file->checkImg();
            $data['type'] = $file->getInfo('type');
            $data['size'] = $file->getInfo('size');

            $info = $file->move('public' . $dir, $saveName);

            $url = 'http://' . $_SERVER['HTTP_HOST']. '/public' . $dir . $saveName;
        }

        if ($info) {
            // 上传成功
            $msg['msg'] = '上传成功';
            $msg['url'] = $url;
            return $this->successJson($msg);
        } else {
            // 上传失败获取错误信息
            $msg['msg'] = '上传失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 后台上传图片到本地
     * @description 后台上传图片到本地(会返回图片保存的地址,将返回的地址传入其他接口即可)
     * @author 李锐志
     * @url /upload/upload/upload_admin
     * @method *
     */
    public function upload_admin()
    {
        // 获取表单上传文件
        $files = request()->file();

        if (!$files) {
            // 上传失败获取错误信息
            $msg['msg'] = '上传为空';
            return $this->errorJson($msg);
        }

        foreach ($files as $file) {
            // 移动到框架应用根目录/public/upload/ 目录下
            $dir = '/upload/';
            $extension = strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION));
            if ($extension) {
                $saveName = md5(microtime(true)) . "." . $extension;
            } else {
                $saveName = md5(microtime(true));
            }

            $data['oname'] = $file->getInfo('name');
            $data['isimg'] = $file->checkImg();
            $data['type'] = $file->getInfo('type');
            $data['size'] = $file->getInfo('size');

            $info = $file->move('public' . $dir, $saveName);

            $url = 'http://' . $_SERVER['HTTP_HOST']. '/public' . $dir . $saveName;
        }

        if ($info) {
            // 上传成功
            $msg['msg'] = '上传成功';
            $msg['url'] = $url;
            return $this->successJson($msg);
        } else {
            // 上传失败获取错误信息
            $msg['msg'] = '上传失败';
            return $this->errorJson($msg);
        }
    }
}